package com.zzqstart.cateservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzqstart.cateservice.entity.CateCategory;
import com.zzqstart.cateservice.entity.vo.OneCategoryVo;
import com.zzqstart.cateservice.entity.vo.TwoCategoryVo;
import com.zzqstart.cateservice.mapper.CateCategoryMapper;
import com.zzqstart.cateservice.service.CateCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@Service
public class CateCategoryServiceImpl extends ServiceImpl<CateCategoryMapper, CateCategory> implements CateCategoryService {

    @Override
    public List<OneCategoryVo> getAllCategoryTree() {
        List<OneCategoryVo> returnCategoryVolist = new ArrayList<>();

        // 查询一级
        QueryWrapper<CateCategory> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id",0);
        List<CateCategory> oneSubjects = baseMapper.selectList(oneWrapper);

        // 查询二级
        QueryWrapper<CateCategory> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id",0);
        List<CateCategory> twoSubjects = baseMapper.selectList(twoWrapper);


        for (int i = 0; i < oneSubjects.size(); i++) {
            // 一级装配
            CateCategory oneSubject = oneSubjects.get(i);
            OneCategoryVo oneCategoryVo = new OneCategoryVo();
            BeanUtils.copyProperties(oneSubject,oneCategoryVo);
            returnCategoryVolist.add(oneCategoryVo);

            ArrayList<TwoCategoryVo> twoCategoryVoS = new ArrayList<>();


            //二级装配
            for (int j = 0; j < twoSubjects.size(); j++) {
                CateCategory twoSubject = twoSubjects.get(j);

                if (twoSubject.getParentId().equals(oneSubject.getId())){
                    TwoCategoryVo twoCategoryVo = new TwoCategoryVo();
                    BeanUtils.copyProperties(twoSubject,twoCategoryVo);

                    twoCategoryVoS.add(twoCategoryVo);
                }
            }
            oneCategoryVo.setChildren(twoCategoryVoS);

        }

        return returnCategoryVolist;
    }

    @Override
    public List<CateCategory> getAllCategory() {
        List<CateCategory> list = baseMapper.selectList(new QueryWrapper<>());
        return list;
    }

    @Override
    public List<String> getChildrenIdListById(String id) {
        QueryWrapper<CateCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        wrapper.select("id");
        List<CateCategory> cateCategoryList = baseMapper.selectList(wrapper);
        List<String> list = new ArrayList<>();
        for (CateCategory category : cateCategoryList) {
            String categoryId = category.getId();
            list.add(categoryId);
        }
        return list;
    }

}
